<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/12/1
 * Time: 15:25
 */
namespace zhtfrontend\modules\sitepadapi\controllers;

use zhtfrontend\modules\sitepadapi\components\rest\ActivebackController;
use yii\web\Response;
use yii\data\ArrayDataProvider;
use yii\web\NotFoundHttpException;

class NoticeController extends ActivebackController
{
    public $modelClass = 'common\models\datas\Message';

    public $serializer = [
        'class' => 'zhtfrontend\modules\sitepadapi\components\rest\Serializer',
        'collectionEnvelope' => 'items'
    ];

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        return $behaviors;
    }

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['create'], $actions['update'], $actions['delete'], $actions['view']);
        return $actions;
    }

    public function actionFindbyrt($receiver_id, $time = '')
    {
        $modelClass = $this->modelClass;
        $query = $modelClass::find()
            ->where(['receiver_id' => $receiver_id])->andWhere(['or', 'status=1', 'status=0'])->all();
        $data = [];
        foreach ($query as $k => $v) {
            if (!empty($time) && $time == \Yii::$app->formatter->asDate($v['created_at'], 'M/d')) {
                $data[] = [
                    'img' => '',
                    'message_id' => $v['id'],
                    'type_id' => $v['type_id'],
                    'title' => $this->getType($v['type_id']),
                    'content' => $v['content'],
                    'time' => $this->getTime($v['created_at']),
                    'status' => $v['status']
                ];
            }
            if (empty($time)) {
                $data[] = [
                    'img' => '',
                    'message_id' => $v['id'],
                    'type_id' => $v['type_id'],
                    'title' => $this->getType($v['type_id']),
                    'content' => $v['content'],
                    'time' => $this->getTime($v['created_at']),
                    'status' => $v['status']
                ];
            }
        }
        return new ArrayDataProvider([
            'allModels' => $data,
            'pagination' => ['pageSize' => 20]
        ]);
    }

    public function actionNoticetime($receiver_id)
    {
        $data = [];
        $modelClass = $this->modelClass;
        $query = $modelClass::find()
            ->where(['receiver_id' => $receiver_id])->andWhere(['or', 'status=1', 'status=0'])
            ->orderBy('created_at DESC')
            ->all();
        foreach ($query as $k => $v) {
            $data[] = \Yii::$app->formatter->asDate($v['created_at'], 'M/d');
        }

        return ['items' => array_values(array_unique($data))];
    }

    public function actionNoticecount($receiver_id)
    {
        $modelClass = $this->modelClass;
        $query = $modelClass::find()
            ->where(['receiver_id' => $receiver_id, 'status' => 0])->count();
        if($query<1) return ['items'=>['total_number'=>'']];
        return ['items' => ['total_number' => $query]];
    }


    public function actionView($id)
    {
        $data = $this->findModel($id);
        $data->status = 1;
        $data->save(false);
        return ['items' => [
            'title' => 'test',
            'content' => $data['content'],
            'time' => \Yii::$app->formatter->asDatetime($data['created_at'], 'yyyy-MM-dd h:i:s')
        ]];
    }

    public function actionDel($id)
    {
        $model = $this->findModel($id);
        $model->status = 2;
        $model->save(false);
        return ['items' => $model];
    }

    protected function findModel($id)
    {
        $modelClass = $this->modelClass;
        if (($model = $modelClass::findOne($id)) !== null) {
            return $model;
        } else {
            try {
                throw new NotFoundHttpException('信息不存在');
            } catch (NotFoundHttpException $e) {
                return ['status' => 0, 'message' => 'error', 'result' => $e->getMessage()];
            }
        }
    }

    protected function getTime($time)
    {
        $yesterdayTime = date("Y-m-d", strtotime("-1 day"));
        $createdTime = \Yii::$app->formatter->asDate($time, 'yyyy-MM-dd');
        if ($yesterdayTime == $createdTime) {
            return '昨天';
        }
        return $createdTime;
    }

    protected function getType($type_id)
    {
        switch ($type_id) {
            case 1:
                return '家委会';
                break;
            case 2:
                return '成长日记';
                break;
            case 3:
                return '点评';
                break;
            case 4:
                return '账户信息';
                break;
            case 5:
                return '个人资料';
                break;
            case 6:
                return '积分';
                break;
            case 7:
                return '课程';
                break;
            case 8:
                return '公告';
                break;
            case 9:
                return '活动消息';
                break;
        }
    }


}